package com.mall.implementation;

import java.util.ArrayList;




import com.mall.core.InventoryController;
import com.mall.implementation.InventoryImpl;
import com.mall.core.Inventory;
import com.mall.core.Item;


public class InventoryControllerImpl implements InventoryController{
	protected InventoryControllerImpl(){}
    private ArrayList<Inventory> inventories;
	@Override
	public void addItemtoInventory(Item item,int storeownerid) {
		for(Inventory inventoryy : inventories)	{
	    	if (inventoryy.getStoreOwnerId()==storeownerid){
	    		inventoryy.addaNewItemEntry(item);
	    	}
	    }
	}

	@Override
	public void addItemstoInventory(ArrayList<Item> item,int storeownerid) {
		for(Inventory inventoryy : inventories)	{
	    	if (inventoryy.getStoreOwnerId()==storeownerid){
	    		inventoryy.addNewItems(item);
	    	}
	    }
	}

	@Override
	public void removeItem(Item item,int storeownerid) {
		for(Inventory inventoryy : inventories)	{
	    	if (inventoryy.getStoreOwnerId()==storeownerid){
	    		inventoryy.deleteItem(item);
	    	}
	    }
		
	}

	@Override
	public void removeItem(int itemid,int storeownerid) {
		for(Inventory inventoryy : inventories)	{
	    	if (inventoryy.getStoreOwnerId()==storeownerid){
	    		inventoryy.deleteItem(itemid);
	    	}
	    }	
		
	}

	@Override
	public int countInventoryItems(int storeownerid) {
		int count=0;
		for(Inventory inventoryy : inventories)	{
	    	if (inventoryy.getStoreOwnerId()==storeownerid){
	    		count = inventoryy.getSize();
	    	}
	    }	
		return count;
	}


	@Override
	public void createInventory(int storeid, int storeowner) {
if(inventories.size()!=0){
	for(Inventory inventoryy : inventories){
		if(inventoryy.getStoreOwnerId()!=storeowner&& inventoryy.getStoreId()!=storeid){
		inventories.add(new InventoryImpl(storeid,storeowner));
		}
	}
}
else{
	inventories.add(new InventoryImpl(storeid,storeowner));
}
	}

	@Override
	public int getInventoryOwnerId(Inventory inventory) {
		int storeownerid =0;
    for(Inventory inventoryy : inventories)	{
    	if (inventory.getStoreOwnerId()==inventoryy.getStoreOwnerId()){
    		storeownerid = inventoryy.getStoreOwnerId();
    	}
    }	
	return storeownerid;
	}

	@Override
	public boolean setInventoryOwnerId(int ownerid) {
		boolean done =false;
	    for(Inventory inventoryy : inventories)	{
	    	if (inventoryy.getStoreOwnerId()==inventoryy.getStoreOwnerId()){
	    		inventoryy.setStoreOwnerId(ownerid);
	    		done =true;
	    	}
	    }	
		return done;
	}

	@Override
	public int getStoreOfInventory(Inventory inventory) {
		int ownerid =0;
	    for(Inventory inventoryy : inventories)	{
	    	if (inventory.getStoreId()==inventoryy.getStoreOwnerId()){
	    		ownerid = inventoryy.getStoreId();
	    	}
	    }	
		return ownerid;
	}

	@Override
	public boolean setStoreIdOfInventory(Inventory inventory, int storeid) {
		boolean done =false;
	    for(Inventory inventoryy : inventories)	{
	    	if (inventory.getStoreId()==inventoryy.getStoreId()){
	    		inventoryy.setStoreId(storeid);
	    		done =true;
	    	}
	    }	
		return done;
	}

	

	@Override
	public void updateInventoryItem(Item itemtoupdate,
			Item updateditem,int storeownerid) {
		for(Inventory inventoryy : inventories)	{
	    	if (inventoryy.getStoreOwnerId()==storeownerid){
	    		if(inventoryy.FindItem(itemtoupdate)){
	    			inventoryy.deleteItem(itemtoupdate);
	    			inventoryy.addaNewItemEntry(updateditem);
	    		}
	    	}
	    }
			
	}

	@Override
	public Item getInventoryItems(int storeownerid, Item item) {
		Item returnitem=null;
		for(Inventory inventoryy : inventories)	{
	    	if (inventoryy.getStoreOwnerId()==storeownerid){
	    		returnitem = inventoryy.getItem(item);
	    	}
	    }	
		return returnitem;
	}


	
}
